package vg.modules.attribute_panel.components;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import vg.modules.attribute_panel.components.data.AttributeShower;
import vg.modules.attribute_panel.components.data.AttributeValue;

public class AttributeTableCellRenderer implements TableCellRenderer { 
	public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();

	public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) { 
		switch(column) {
			case 0:
			{
				JCheckBox checkBox = new JCheckBox();
				AttributeShower shower = (AttributeShower)table.getValueAt(row, 0);
				checkBox.setBackground(shower.getColor());
				checkBox.setSelected(shower.isShow());
				checkBox.addActionListener(new FirstColumnActionListener(shower));
				return(checkBox);
			}
			case 2:
			{
				AttributeValue v = (AttributeValue)table.getValueAt(row, 2);
				List<String>lv = v.getData();
				JComponent component;
				if(lv.size()==1) {
					Object obj = lv.get(0);
					component = new JLabel((String)obj);
				} else {
					component = new JComboBox();
					for(Object buf : lv) {
						((JComboBox)component).addItem((String)buf);
					}
				}
				return(component);
			}
		}
		return(null);
	}
}
/**
 * Action listener for first column(show or unshow attribute).
 * @author tzolotuhin
 */
class FirstColumnActionListener implements ActionListener {
	private final AttributeShower shower;
	/**
	 * Constructor.
	 */
	public FirstColumnActionListener(final AttributeShower shower) {
		this.shower = shower;
	}
	/**
	 * Action
	 */
	public void actionPerformed(ActionEvent e) {
		JCheckBox checkBox = (JCheckBox)e.getSource();
		if(checkBox.isSelected()) {
			shower.show();
		} else {
			shower.unshow();
		}
	}
}